package top.smallway.UI;

import top.smallway.Myiews.Dialog;
import top.smallway.Myiews.PasswordField;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Register extends JFrame {
    private boolean isDraging;
    private int xx, yy;

    public Register() {
        setSize(775, 794);
        setUndecorated(true);
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();  //获取屏幕对象
        setBackground(new Color(0, 0, 0, 0));
        int x = (int) screensize.getWidth() / 2 - getWidth() / 2;
        int y = (int) screensize.getHeight() / 2 - getHeight() / 2;
        setLocation(x, y);
        setLocationRelativeTo(null);  //自动居中对齐
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                isDraging = true;
                xx = e.getX();
                yy = e.getY();
            }


            @Override
            public void mouseReleased(MouseEvent e) {
                isDraging = false;
            }
        });
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDraging) {
                    int left = getX();
                    int top = getY();
                    setLocation(left + e.getX() - xx, top + e.getY() - yy);
                }
            }
        });
        Panels total = new Panels();
        total.setOpaque(false);

        addViews(total);

        this.add(total);
        this.setVisible(true);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

    }

    private void addViews(Panels total) {
        total.setLayout(null);
        JLabel title = new JLabel("用户注册");
        title.setFont(new Font("楷体", Font.BOLD, 56));
        title.setForeground(Color.WHITE);
        title.setBounds(280, 87, 300, 100);
        total.add(title);

        JTextField user_name = new JTextField("姓名");
        user_name.setFont(new Font("楷体", Font.PLAIN, 36));
        user_name.setBounds(150, 170, 500, 60);
        user_name.setForeground(Color.gray);
        total.add(user_name);
        user_name.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (user_name.getText().equals("姓名")) {
                    user_name.setText("");     //将提示文字清空
                    user_name.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (user_name.getText().equals("")) {
                    user_name.setForeground(Color.gray); //将提示文字设置为灰色
                    user_name.setText("姓名");     //显示提示文字
                }

            }
        });

        JTextField phone_number = new JTextField("手机号");
        phone_number.setFont(new Font("楷体", Font.PLAIN, 36));
        phone_number.setBounds(150, 270, 500, 60);
        phone_number.setForeground(Color.gray);
        total.add(phone_number);
        phone_number.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (phone_number.getText().equals("手机号")) {
                    phone_number.setText("");     //将提示文字清空
                    phone_number.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (phone_number.getText().equals("")) {
                    phone_number.setForeground(Color.gray); //将提示文字设置为灰色
                    phone_number.setText("手机号");     //显示提示文字
                }

            }
        });

        JTextField email = new JTextField("电子邮箱");
        email.setFont(new Font("楷体", Font.PLAIN, 36));
        email.setBounds(150, 370, 500, 60);
        email.setForeground(Color.gray);
        total.add(email);
        email.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (email.getText().equals("电子邮箱")) {
                    email.setText("");     //将提示文字清空
                    email.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (email.getText().equals("")) {
                    email.setForeground(Color.gray); //将提示文字设置为灰色
                    email.setText("电子邮箱");     //显示提示文字
                }

            }
        });

        PasswordField password = new PasswordField();
        password.setForeground(Color.gray); //将提示文字设置为灰色
        password.setText("密码");
        password.setEchoChar((char) 0);
        password.setFont(new Font("楷体", Font.PLAIN, 36));
        password.setBounds(150, 470, 500, 60);
        total.add(password);
        password.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (String.valueOf(password.getPassword()).equals("密码")) {
                    password.setText("");     //将提示文字清空
                    password.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (String.valueOf(password.getPassword()).equals("")) {
                    password.setForeground(Color.gray); //将提示文字设置为灰色
                    password.setText("密码");
                    password.setEchoChar((char) 0);

                }

            }
        });

        PasswordField confirm_password = new PasswordField();
        confirm_password.setForeground(Color.gray); //将提示文字设置为灰色
        confirm_password.setText("密码");
        confirm_password.setEchoChar((char) 0);
        confirm_password.setFont(new Font("楷体", Font.PLAIN, 36));
        confirm_password.setBounds(150, 570, 500, 60);
        total.add(confirm_password);
        confirm_password.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (String.valueOf(confirm_password.getPassword()).equals("密码")) {
                    confirm_password.setText("");     //将提示文字清空
                    confirm_password.setForeground(Color.black);  //设置用户输入的字体颜色为黑色
                }

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (String.valueOf(confirm_password.getPassword()).equals("")) {
                    confirm_password.setForeground(Color.gray); //将提示文字设置为灰色
                    confirm_password.setText("密码");
                    confirm_password.setEchoChar((char) 0);

                }

            }
        });
        JButton confirm = new JButton("注册");
        confirm.setFont(new Font("楷体", Font.BOLD, 30));
        confirm.setBounds(150, 670, 500, 45);
        confirm.setBackground(new Color(71, 159, 254));  // 你可以选择任何颜色
        confirm.setOpaque(true);
        total.add(confirm);
        confirm.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (password.getHashPassword().equals(confirm_password.getHashPassword())){
//                    TODO 密码相同继续注册
                }else {
                    Dialog dialog = new Dialog();
                    dialog.setVisible(true);
                    dialog.setDefaultCloseOperation(dialog.EXIT_ON_CLOSE);
                }
            }
        });


    }


    static class Panels extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage backgroundImage;
            try {
                backgroundImage = ImageIO.read(new File("src/main/resources/reg_bj.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            // 确保 JPanel 背景是透明的
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制 PNG 图像 (确保透明部分显示)
            if (backgroundImage != null) {
                g2d.drawImage(backgroundImage, 0, 0, null);
            }

        }
    }
}
